﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Bag : MonoBehaviour
{
    public float GapWidth;
    public Transform LDTransform;
    public GameObject BagSlotPrefab;
    public Player Player;
    public Canvas CanvasUnderSprite;
    public Text TextFull;

    private GameObject[] bagSlotO;
    private BagSlot[] bagSlot;
    private int nowSlot;
    // Start is called before the first frame update
    void Start()
    {
        bagSlotO = new GameObject[9];
        bagSlot = new BagSlot[9];
        for(int i = 0; i < 9; i++) {
            bagSlotO[i] = Instantiate(BagSlotPrefab, transform);
            bagSlot[i] = bagSlotO[i].GetComponent<BagSlot>();
            bagSlotO[i].SetActive(true);
            bagSlotO[i].transform.position = LDTransform.position;
        }
        Vector3 dx = new Vector3(GapWidth, 0, 0), dy = new Vector3(0, GapWidth, 0);
        bagSlotO[1].transform.position += dx;
        bagSlotO[2].transform.position += dx * 2;
        bagSlotO[3].transform.position += dy;
        bagSlotO[4].transform.position += dx + dy;
        bagSlotO[5].transform.position += dx * 2 + dy;
        bagSlotO[6].transform.position += dy * 2;
        bagSlotO[7].transform.position += dx + dy * 2;
        bagSlotO[8].transform.position += dx * 2 + dy * 2;
        for(int i = 0; i < 9; i++)
            Player.AddTeris(0);
    }

    // Update is called once per frame
    void Update()
    {
        if(Time.timeScale == 0) {
            if(Input.GetKeyDown(KeyCode.Keypad0) || Input.GetKeyDown(KeyCode.Alpha0)) {
                Destroy(tempPreviewO);
                tempPreviewO = null;
                tempTeris = null;
                Time.timeScale = 1f;
                CanvasUnderSprite.gameObject.SetActive(false);
                TextFull.gameObject.SetActive(false);
            }
            else if(Input.GetKeyDown(KeyCode.Keypad1) || Input.GetKeyDown(KeyCode.Alpha1)) {
                DestroyTeris(0);
            }
            else if(Input.GetKeyDown(KeyCode.Keypad2) || Input.GetKeyDown(KeyCode.Alpha2)) {
                DestroyTeris(1);
            }
            else if(Input.GetKeyDown(KeyCode.Keypad3) || Input.GetKeyDown(KeyCode.Alpha3)) {
                DestroyTeris(2);
            }
            else if(Input.GetKeyDown(KeyCode.Keypad4) || Input.GetKeyDown(KeyCode.Alpha4)) {
                DestroyTeris(3);
            }
            else if(Input.GetKeyDown(KeyCode.Keypad5) || Input.GetKeyDown(KeyCode.Alpha5)) {
                DestroyTeris(4);
            }
            else if(Input.GetKeyDown(KeyCode.Keypad6) || Input.GetKeyDown(KeyCode.Alpha6)) {
                DestroyTeris(5);
            }
            else if(Input.GetKeyDown(KeyCode.Keypad7) || Input.GetKeyDown(KeyCode.Alpha7)) {
                DestroyTeris(6);
            }
            else if(Input.GetKeyDown(KeyCode.Keypad8) || Input.GetKeyDown(KeyCode.Alpha8)) {
                DestroyTeris(7);
            }
            else if(Input.GetKeyDown(KeyCode.Keypad9) || Input.GetKeyDown(KeyCode.Alpha9)) {
                DestroyTeris(8);
            }
        }
    }

    private void DestroyTeris(int terisNum) {
        Destroy(bagSlot[terisNum].PreviewO);
        bagSlot[terisNum].PreviewO = null;
        bagSlot[terisNum].TerisInstance = null;

        bagSlot[terisNum].TerisInstance = tempTeris;
        bagSlot[terisNum].PreviewO = Instantiate(tempPreviewO, bagSlotO[terisNum].transform);
        bagSlot[terisNum].PreviewO.transform.position = bagSlotO[terisNum].transform.position + new Vector3(0.26f, GapWidth * 0.7f, 0);
        bagSlot[terisNum].PreviewO.transform.localScale = new Vector3(0.7f, 0.7f, 1f);

        Destroy(tempPreviewO);
        tempPreviewO = null;
        tempTeris = null;
        Time.timeScale = 1f;
        CanvasUnderSprite.gameObject.SetActive(false);
        TextFull.gameObject.SetActive(false);
    }

    private int GetFirstEmpty() {
        for(int i = 0; i < 9; i++) {
            if(bagSlot[i].TerisInstance == null)
                return i;
        }
        return -1;
    }

    private TerisInstance tempTeris;
    private GameObject tempPreviewO;
    public void AddTeris(TerisInstance newTeris, GameObject LTPoint) {
        int pos = GetFirstEmpty();
        if(pos == -1) {
            tempTeris = newTeris;
            tempPreviewO = Instantiate(LTPoint);
            tempPreviewO.transform.position = new Vector3(-3.4f, 0.1f, 0);
            CanvasUnderSprite.gameObject.SetActive(true);
            TextFull.gameObject.SetActive(true);
            Time.timeScale = 0;
        }
        else {
            bagSlot[pos].TerisInstance = newTeris;
            bagSlot[pos].PreviewO = Instantiate(LTPoint, bagSlotO[pos].transform);
            bagSlot[pos].PreviewO.transform.position = bagSlotO[pos].transform.position + new Vector3(0.26f, GapWidth * 0.7f, 0);
            bagSlot[pos].PreviewO.transform.localScale = new Vector3(0.7f, 0.7f, 1f);
        }
    }

    public int GetFirstTeirsNum() {
        for(int i = 0; i < 9; i++) {
            if(bagSlot[i].TerisInstance != null)
                return i;
        }
        return -1;
    }

    public bool Exist(int terisNum) {
        return bagSlot[terisNum].TerisInstance != null;
    }

    public TerisInstance GetTeris(int terisNum) {
        nowSlot = terisNum;
        return bagSlot[terisNum].TerisInstance;
    }

    public void DestroyCurrent() {
        Destroy(bagSlot[nowSlot].PreviewO);
        bagSlot[nowSlot].PreviewO = null;
        bagSlot[nowSlot].TerisInstance = null;
    }
}
